package com.demo2do.fms.admin.web.controller.supplier;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.utils.WebUtils;
import com.demo2do.fms.admin.entity.search.supplier.CommonStorageSupplierSearch;
import com.demo2do.fms.admin.entity.security.Login;
import com.demo2do.fms.admin.service.supplier.SupplierInfoService;
import com.demo2do.fms.admin.service.supplier.SupplierService;
import com.demo2do.fms.admin.web.resolver.Secure;
import com.demo2do.fms.entity.supplier.common.Supplier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @description: 一般仓储供方
 * @author: 11200
 * @date: 2019/7/22
 */
@Controller
@RequestMapping(path = "common-storage-suppliers")
@MenuSetting("menu-supplier")
public class CommonStorageSupplierController {

    @Autowired
    private SupplierService supplierService;

    @RequestMapping(path = "/search", method = RequestMethod.GET)
    public ModelAndView search() {

        ModelAndView modelAndView = new ModelAndView("supplier/common-storage-supplier/common-storage-supplier-search");

        return modelAndView;
    }

    /**
     * list
     *
     * @param login
     * @param page
     * @param request
     * @return
     */
    @RequestMapping(path = "", method = RequestMethod.GET)
    @MenuSetting("submenu-common-storage-list")
    public ModelAndView list(@Secure Login login, Page page, CommonStorageSupplierSearch commonStorageSupplierSearch, HttpServletRequest request) {

        ModelAndView modelAndView = new ModelAndView("supplier/common-storage-supplier/common-storage-supplier-list");

        int totals = supplierService.count(Supplier.class, commonStorageSupplierSearch.getPreparedFilter(false));
        if(totals > 0){
            List<Supplier> suppliers = supplierService.search(Supplier.class, commonStorageSupplierSearch, page.with(totals));
            modelAndView.addObject("page", page);
            modelAndView.addObject("suppliers", suppliers);
            modelAndView.addObject("queryString", WebUtils.extractQueryString(request, "page"));
        }
        return modelAndView;

    }

}
